//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using Mindscape.BackgroundMotion.Website.Controllers;
using Mindscape.BackgroundMotion.Website.Infrastructure;
using Mindscape.BackgroundMotion.Model;

namespace Mindscape.BackgroundMotion.Website.Views
{
  public sealed class SignupPresenter : PresenterBase<ISignupView, IMembershipController>
  {
    /// <summary>
    /// This method is invoked by the view everytime it loads
    /// </summary>
    public override void OnViewLoaded()
    {
      View.CurrentMember = new Member();
    }

    /// <summary>
    /// Saves the new <see ref="Member"/> instance.
    /// </summary>
    public void Save()
    {
      if (View.CurrentMember.ValidationResults.IsValid)
      {
        Controller.CreateAccount(View.CurrentMember);
        Controller.Authenticate(View.CurrentMember.Email, View.CurrentMember.Password, false);
        Controller.RedirectTo("~/Add.aspx");
      }
      else
      {
        View.ValidationResults = View.CurrentMember.ValidationResults;
      }
    }
  }
}